//
//  CourseViewController.m
//  NoNameAmazing
//
//  Created by hero on 2017/9/8.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//  课程

#import "CourseViewController.h"
#import "GFCalendarView.h"
#import "CourseModel.h"
#import "CourseCell.h"
@interface CourseViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataSource;
@property (nonatomic ,strong) GFCalendarView *calendar;
@end

@implementation CourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseCell" bundle:nil] forCellReuseIdentifier:@"CourseCell"];
    [self prepareUI];
    
}
- (void)prepareUI{//
    CGFloat width = self.view.bounds.size.width ;
    CGPoint origin = CGPointMake(0, 0);
    _calendar = [[GFCalendarView alloc] initWithFrameOrigin:origin width:width];
    // 点击某一天的回调
    _calendar.didSelectDayHandler = ^(NSInteger year, NSInteger month, NSInteger day) {
        
        NSString * monthStr = [NSString stringWithFormat:@"%ld",month];
        NSString *dayStr = [NSString stringWithFormat:@"%ld",day];
        if (monthStr.length<2) {
            monthStr = [NSString stringWithFormat:@"0%@",monthStr];
        }
        if (dayStr.length<2) {
            dayStr = [NSString stringWithFormat:@"0%@",dayStr];
        }
        typeof(self) ws = self;
        NSString *title = [NSString stringWithFormat:@"%ld年%ld月%ld日", year, month,day];
        
        [ws.calendar.calendarHeaderButton setTitle:title forState:UIControlStateNormal];
        [self loadDataWithParams:[NSString stringWithFormat:@"%ld%@%@",year,monthStr,dayStr]];
    };
    [self.view addSubview:_calendar];
}

#pragma mark - tableViewDelegate -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseCell"];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)loadDataWithParams:(NSString *)str{
    self.dataSource = nil;
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid],@"date":str};
    [NetWorkManager requestWithType:1 withUrlString:@"/course/getCoursebyDay.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue] == 1) {
            _calendar.bgTitle = [NSString stringWithFormat:@"共%ld节课程",[object[@"courseList"] count]];
            for (NSDictionary *dict in object[@"courseList"]) {
                CourseModel * model = [CourseModel modelWithDict:dict];
                [self.dataSource addObject:model];
            }
        }else if ([object[@"status"] integerValue] == 0){
            _calendar.bgTitle = object[@"message"];
        }
        [self.tableView reloadData];
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}
#pragma mark - lazyinit -
- (UITableView *)tableView{
    if (!_tableView) {
        CGFloat weekLineHight = 0.85 * (kScreen_Width / 7.0);
        CGFloat monthHeight = 5 * weekLineHight;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,100+monthHeight+42.5, kScreen_Width, kScreen_Height-(100+monthHeight)-44-44) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
